{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:16:58.738417Z",
     "start_time": "2020-07-24T02:16:57.290354Z"
    }
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "from datetime import datetime, timedelta\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "from tqdm import tqdm\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "\n",
    "import gc\n",
    "import os\n",
    "\n",
    "import lightgbm as lgb\n",
    "from sklearn.model_selection import StratifiedKFold, KFold\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from gensim.models import Word2Vec\n",
    "\n",
    "warnings.simplefilter('ignore')\n",
    "tqdm.pandas()\n",
    "%matplotlib inline\n",
    "\n",
    "pd.set_option('max_columns', None)\n",
    "pd.set_option('max_rows', 300)\n",
    "pd.set_option('max_colwidth', 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:16:58.743654Z",
     "start_time": "2020-07-24T02:16:58.740879Z"
    }
   },
   "outputs": [],
   "source": [
    "seed = 2020"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:01.371573Z",
     "start_time": "2020-07-24T02:16:58.745687Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "df_train_label = pd.read_csv('raw_data/train/train_label.csv')\n",
    "df_train_base = pd.read_csv('raw_data/train/train_base.csv')\n",
    "df_train_trans = pd.read_csv('raw_data/train/train_trans.csv')\n",
    "\n",
    "df_test_base = pd.read_csv('raw_data/test_a/test_a_base.csv')\n",
    "df_test_trans = pd.read_csv('raw_data/test_a/test_a_trans.csv')\n",
    "\n",
    "df_trans = df_train_trans.append(df_test_trans)\n",
    "df_trans = df_trans.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:21.624028Z",
     "start_time": "2020-07-24T02:17:01.373746Z"
    }
   },
   "outputs": [],
   "source": [
    "def parse_time(tm):\n",
    "    days, _, time = tm.split(' ')\n",
    "    time = time.split('.')[0]\n",
    "\n",
    "    time = '2020-1-1 ' + time\n",
    "    time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')\n",
    "    time = (time + timedelta(days=int(days)))\n",
    "\n",
    "    return time\n",
    "\n",
    "\n",
    "df_trans['date'] = df_trans['tm_diff'].apply(parse_time)\n",
    "df_trans['day'] = df_trans['date'].dt.day\n",
    "df_trans['hour'] = df_trans['date'].dt.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:22.412685Z",
     "start_time": "2020-07-24T02:17:21.626211Z"
    }
   },
   "outputs": [],
   "source": [
    "df_trans.sort_values(['user', 'date'], inplace=True)\n",
    "df_trans = df_trans.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:22.445619Z",
     "start_time": "2020-07-24T02:17:22.414819Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>platform</th>\n",
       "      <th>tunnel_in</th>\n",
       "      <th>tunnel_out</th>\n",
       "      <th>amount</th>\n",
       "      <th>type1</th>\n",
       "      <th>ip</th>\n",
       "      <th>type2</th>\n",
       "      <th>ip_3</th>\n",
       "      <th>tm_diff</th>\n",
       "      <th>date</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>TestA_00001</td>\n",
       "      <td>46c69cbbce5f1568</td>\n",
       "      <td>b2e7fa260df4998d</td>\n",
       "      <td>6ee790756007e69a</td>\n",
       "      <td>84299</td>\n",
       "      <td>45a1168437c708ff</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11a213398ee0c623</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2 days 09:38:22.000000000</td>\n",
       "      <td>2020-01-03 09:38:22</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>TestA_00001</td>\n",
       "      <td>46c69cbbce5f1568</td>\n",
       "      <td>b2e7fa260df4998d</td>\n",
       "      <td>6ee790756007e69a</td>\n",
       "      <td>100537</td>\n",
       "      <td>45a1168437c708ff</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11a213398ee0c623</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19 days 12:50:46.000000000</td>\n",
       "      <td>2020-01-20 12:50:46</td>\n",
       "      <td>20</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>TestA_00001</td>\n",
       "      <td>46c69cbbce5f1568</td>\n",
       "      <td>b2e7fa260df4998d</td>\n",
       "      <td>6ee790756007e69a</td>\n",
       "      <td>103071</td>\n",
       "      <td>45a1168437c708ff</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11a213398ee0c623</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19 days 12:50:47.000000000</td>\n",
       "      <td>2020-01-20 12:50:47</td>\n",
       "      <td>20</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>TestA_00001</td>\n",
       "      <td>46c69cbbce5f1568</td>\n",
       "      <td>b2e7fa260df4998d</td>\n",
       "      <td>6ee790756007e69a</td>\n",
       "      <td>47289</td>\n",
       "      <td>45a1168437c708ff</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11a213398ee0c623</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20 days 12:33:42.000000000</td>\n",
       "      <td>2020-01-21 12:33:42</td>\n",
       "      <td>21</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>TestA_00002</td>\n",
       "      <td>42573d7287a8c9c2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6ee790756007e69a</td>\n",
       "      <td>41187</td>\n",
       "      <td>f67d4b5a05a1352a</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11 days 17:51:53.000000000</td>\n",
       "      <td>2020-01-12 17:51:53</td>\n",
       "      <td>12</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          user          platform         tunnel_in        tunnel_out  amount  \\\n",
       "0  TestA_00001  46c69cbbce5f1568  b2e7fa260df4998d  6ee790756007e69a   84299   \n",
       "1  TestA_00001  46c69cbbce5f1568  b2e7fa260df4998d  6ee790756007e69a  100537   \n",
       "2  TestA_00001  46c69cbbce5f1568  b2e7fa260df4998d  6ee790756007e69a  103071   \n",
       "3  TestA_00001  46c69cbbce5f1568  b2e7fa260df4998d  6ee790756007e69a   47289   \n",
       "4  TestA_00002  42573d7287a8c9c2               NaN  6ee790756007e69a   41187   \n",
       "\n",
       "              type1   ip             type2 ip_3                     tm_diff  \\\n",
       "0  45a1168437c708ff  NaN  11a213398ee0c623  NaN   2 days 09:38:22.000000000   \n",
       "1  45a1168437c708ff  NaN  11a213398ee0c623  NaN  19 days 12:50:46.000000000   \n",
       "2  45a1168437c708ff  NaN  11a213398ee0c623  NaN  19 days 12:50:47.000000000   \n",
       "3  45a1168437c708ff  NaN  11a213398ee0c623  NaN  20 days 12:33:42.000000000   \n",
       "4  f67d4b5a05a1352a  NaN               NaN  NaN  11 days 17:51:53.000000000   \n",
       "\n",
       "                 date  day  hour  \n",
       "0 2020-01-03 09:38:22    3     9  \n",
       "1 2020-01-20 12:50:46   20    12  \n",
       "2 2020-01-20 12:50:47   20    12  \n",
       "3 2020-01-21 12:33:42   21    12  \n",
       "4 2020-01-12 17:51:53   12    17  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_trans.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:22.668988Z",
     "start_time": "2020-07-24T02:17:22.448790Z"
    }
   },
   "outputs": [],
   "source": [
    "df_train = df_train_base.merge(df_train_label, how='left')\n",
    "df_test = df_test_base\n",
    "\n",
    "df_feature = df_train.append(df_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-20T02:42:35.266931Z",
     "start_time": "2020-07-20T02:42:35.263531Z"
    }
   },
   "source": [
    "# 特征工程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 交易信息特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:22.900785Z",
     "start_time": "2020-07-24T02:17:22.671471Z"
    }
   },
   "outputs": [],
   "source": [
    "df_temp = df_trans.groupby(['user'\n",
    "                            ])['amount'].agg(amount_mean='mean',\n",
    "                                             amount_std='std',\n",
    "                                             amount_sum='sum',\n",
    "                                             amount_max='max',\n",
    "                                             amount_min='min').reset_index()\n",
    "df_feature = df_feature.merge(df_temp, how='left')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-20T08:57:03.376937Z",
     "start_time": "2020-07-20T08:57:03.373425Z"
    }
   },
   "source": [
    "## 基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:24.036189Z",
     "start_time": "2020-07-24T02:17:22.902857Z"
    }
   },
   "outputs": [],
   "source": [
    "for f in [\n",
    "        'balance', 'balance_avg', 'balance1', 'balance1_avg', 'balance2',\n",
    "        'balance2_avg', 'product1_amount', 'product2_amount',\n",
    "        'product3_amount', 'product4_amount', 'product5_amount', 'product6_amount'\n",
    "]:\n",
    "    df_feature[f] = df_feature[f].apply(lambda x: int(x.split(' ')[1]) if type(x) != float else np.NaN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:24.051777Z",
     "start_time": "2020-07-24T02:17:24.038308Z"
    }
   },
   "outputs": [],
   "source": [
    "df_feature['product7_fail_ratio'] = df_feature[\n",
    "    'product7_fail_cnt'] / df_feature['product7_cnt']\n",
    "df_feature['card_cnt'] = df_feature['card_a_cnt'] + df_feature[\n",
    "    'card_b_cnt'] + df_feature['card_c_cnt'] + df_feature['card_d_cnt']\n",
    "\n",
    "df_feature['acc_card_ratio'] = df_feature['acc_count'] / df_feature['card_cnt']\n",
    "df_feature['login_cnt'] = df_feature['login_cnt_period1'] + df_feature['login_cnt_period2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:25.940624Z",
     "start_time": "2020-07-24T02:17:24.053652Z"
    },
    "code_folding": [
     0
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 欺诈率\n",
    "def stat(df, df_merge, group_by, agg):\n",
    "    group = df.groupby(group_by).agg(agg)\n",
    "\n",
    "    columns = []\n",
    "    for on, methods in agg.items():\n",
    "        for method in methods:\n",
    "            columns.append('{}_{}_{}'.format('_'.join(group_by), on, method))\n",
    "    group.columns = columns\n",
    "    group.reset_index(inplace=True)\n",
    "    df_merge = df_merge.merge(group, on=group_by, how='left')\n",
    "\n",
    "    del (group)\n",
    "    gc.collect()\n",
    "    return df_merge\n",
    "\n",
    "\n",
    "def statis_feat(df_know, df_unknow):\n",
    "    df_unknow = stat(df_know, df_unknow, ['province'], {'label': ['mean']})\n",
    "    df_unknow = stat(df_know, df_unknow, ['city'], {'label': ['mean']})\n",
    "\n",
    "    return df_unknow\n",
    "\n",
    "\n",
    "df_train = df_feature[~df_feature['label'].isnull()]\n",
    "df_train = df_train.reset_index(drop=True)\n",
    "df_test = df_feature[df_feature['label'].isnull()]\n",
    "\n",
    "df_stas_feat = None\n",
    "kf = StratifiedKFold(n_splits=5, random_state=seed, shuffle=True)\n",
    "for train_index, val_index in kf.split(df_train, df_train['label']):\n",
    "    df_fold_train = df_train.iloc[train_index]\n",
    "    df_fold_val = df_train.iloc[val_index]\n",
    "\n",
    "    df_fold_val = statis_feat(df_fold_train, df_fold_val)\n",
    "    df_stas_feat = pd.concat([df_stas_feat, df_fold_val], axis=0)\n",
    "\n",
    "    del (df_fold_train)\n",
    "    del (df_fold_val)\n",
    "    gc.collect()\n",
    "\n",
    "df_test = statis_feat(df_train, df_test)\n",
    "df_feature = pd.concat([df_stas_feat, df_test], axis=0)\n",
    "df_feature = df_feature.reset_index(drop=True)\n",
    "\n",
    "del (df_stas_feat)\n",
    "del (df_train)\n",
    "del (df_test)\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:26.002547Z",
     "start_time": "2020-07-24T02:17:25.942555Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>provider</th>\n",
       "      <th>level</th>\n",
       "      <th>verified</th>\n",
       "      <th>using_time</th>\n",
       "      <th>regist_type</th>\n",
       "      <th>card_a_cnt</th>\n",
       "      <th>card_b_cnt</th>\n",
       "      <th>card_c_cnt</th>\n",
       "      <th>agreement1</th>\n",
       "      <th>op1_cnt</th>\n",
       "      <th>op2_cnt</th>\n",
       "      <th>card_d_cnt</th>\n",
       "      <th>agreement_total</th>\n",
       "      <th>service1_cnt</th>\n",
       "      <th>service1_amt</th>\n",
       "      <th>service2_cnt</th>\n",
       "      <th>agreement2</th>\n",
       "      <th>agreement3</th>\n",
       "      <th>agreement4</th>\n",
       "      <th>acc_count</th>\n",
       "      <th>login_cnt_period1</th>\n",
       "      <th>login_cnt_period2</th>\n",
       "      <th>ip_cnt</th>\n",
       "      <th>login_cnt_avg</th>\n",
       "      <th>login_days_cnt</th>\n",
       "      <th>province</th>\n",
       "      <th>city</th>\n",
       "      <th>balance</th>\n",
       "      <th>balance_avg</th>\n",
       "      <th>balance1</th>\n",
       "      <th>balance1_avg</th>\n",
       "      <th>balance2</th>\n",
       "      <th>balance2_avg</th>\n",
       "      <th>service3</th>\n",
       "      <th>service3_level</th>\n",
       "      <th>product1_amount</th>\n",
       "      <th>product2_amount</th>\n",
       "      <th>product3_amount</th>\n",
       "      <th>product4_amount</th>\n",
       "      <th>product5_amount</th>\n",
       "      <th>product6_amount</th>\n",
       "      <th>product7_cnt</th>\n",
       "      <th>product7_fail_cnt</th>\n",
       "      <th>label</th>\n",
       "      <th>amount_mean</th>\n",
       "      <th>amount_std</th>\n",
       "      <th>amount_sum</th>\n",
       "      <th>amount_max</th>\n",
       "      <th>amount_min</th>\n",
       "      <th>product7_fail_ratio</th>\n",
       "      <th>card_cnt</th>\n",
       "      <th>acc_card_ratio</th>\n",
       "      <th>login_cnt</th>\n",
       "      <th>province_label_mean</th>\n",
       "      <th>city_label_mean</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Train_41088</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24853</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 2</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24731</td>\n",
       "      <td>category 7</td>\n",
       "      <td>24712</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24712</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>24737</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 1</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24737</td>\n",
       "      <td>25394</td>\n",
       "      <td>25023</td>\n",
       "      <td>24791</td>\n",
       "      <td>24725</td>\n",
       "      <td>24791</td>\n",
       "      <td>c3e48f852a0da7b6</td>\n",
       "      <td>1b25064aa7fe4945</td>\n",
       "      <td>11</td>\n",
       "      <td>8.0</td>\n",
       "      <td>4</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "      <td>category 0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>1.0</td>\n",
       "      <td>44079.000000</td>\n",
       "      <td>6399.927734</td>\n",
       "      <td>132237.0</td>\n",
       "      <td>47774.0</td>\n",
       "      <td>36689.0</td>\n",
       "      <td>0.999757</td>\n",
       "      <td>98836</td>\n",
       "      <td>0.250283</td>\n",
       "      <td>50417</td>\n",
       "      <td>0.360114</td>\n",
       "      <td>0.445455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Train_31781</td>\n",
       "      <td>category 1</td>\n",
       "      <td>24859</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 1</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24717</td>\n",
       "      <td>category 1</td>\n",
       "      <td>24731</td>\n",
       "      <td>24712</td>\n",
       "      <td>24725</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24731</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>24749</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24743</td>\n",
       "      <td>29840</td>\n",
       "      <td>26251</td>\n",
       "      <td>25011</td>\n",
       "      <td>24737</td>\n",
       "      <td>24974</td>\n",
       "      <td>6dd7071b6edc22d2</td>\n",
       "      <td>998bea7dacc1ac2e</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>category 0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>1.0</td>\n",
       "      <td>97000.000000</td>\n",
       "      <td>85970.142515</td>\n",
       "      <td>291000.0</td>\n",
       "      <td>195918.0</td>\n",
       "      <td>40310.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>98874</td>\n",
       "      <td>0.250248</td>\n",
       "      <td>56091</td>\n",
       "      <td>0.270062</td>\n",
       "      <td>0.263736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Train_31874</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24895</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 2</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24707</td>\n",
       "      <td>category 1</td>\n",
       "      <td>24712</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24719</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>24737</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 1</td>\n",
       "      <td>category 1</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24725</td>\n",
       "      <td>25163</td>\n",
       "      <td>25163</td>\n",
       "      <td>24804</td>\n",
       "      <td>24725</td>\n",
       "      <td>24829</td>\n",
       "      <td>71c3649e6dfc18fe</td>\n",
       "      <td>a18aa4ca362cb2c9</td>\n",
       "      <td>6</td>\n",
       "      <td>8.0</td>\n",
       "      <td>11</td>\n",
       "      <td>12.0</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>category 0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>0.0</td>\n",
       "      <td>28347.708333</td>\n",
       "      <td>4623.104598</td>\n",
       "      <td>680345.0</td>\n",
       "      <td>42553.0</td>\n",
       "      <td>24767.0</td>\n",
       "      <td>0.999757</td>\n",
       "      <td>98836</td>\n",
       "      <td>0.250162</td>\n",
       "      <td>50326</td>\n",
       "      <td>0.313309</td>\n",
       "      <td>0.295139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Train_17154</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24846</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 2</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24732</td>\n",
       "      <td>category 7</td>\n",
       "      <td>24719</td>\n",
       "      <td>24719</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24737</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>24749</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 1</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24712</td>\n",
       "      <td>25680</td>\n",
       "      <td>25005</td>\n",
       "      <td>24834</td>\n",
       "      <td>24719</td>\n",
       "      <td>24822</td>\n",
       "      <td>648024953e363510</td>\n",
       "      <td>e174abcd1cd8c033</td>\n",
       "      <td>7</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7</td>\n",
       "      <td>5</td>\n",
       "      <td>category 0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>0.0</td>\n",
       "      <td>54089.933333</td>\n",
       "      <td>36418.750442</td>\n",
       "      <td>811349.0</td>\n",
       "      <td>156321.0</td>\n",
       "      <td>36689.0</td>\n",
       "      <td>0.999757</td>\n",
       "      <td>98850</td>\n",
       "      <td>0.249995</td>\n",
       "      <td>50685</td>\n",
       "      <td>0.215081</td>\n",
       "      <td>0.236908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Train_40531</td>\n",
       "      <td>category 1</td>\n",
       "      <td>24908</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 2</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24728</td>\n",
       "      <td>category 1</td>\n",
       "      <td>24712</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 0</td>\n",
       "      <td>24725</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>24761</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>24706</td>\n",
       "      <td>category 0</td>\n",
       "      <td>category 1</td>\n",
       "      <td>category 1</td>\n",
       "      <td>24725</td>\n",
       "      <td>25577</td>\n",
       "      <td>25048</td>\n",
       "      <td>24761</td>\n",
       "      <td>24743</td>\n",
       "      <td>24761</td>\n",
       "      <td>21c43413032d5522</td>\n",
       "      <td>76cc98309376a6d2</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>category 0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>24712</td>\n",
       "      <td>24706</td>\n",
       "      <td>1.0</td>\n",
       "      <td>404972.666667</td>\n",
       "      <td>419810.655236</td>\n",
       "      <td>1214918.0</td>\n",
       "      <td>858932.0</td>\n",
       "      <td>30746.0</td>\n",
       "      <td>0.999757</td>\n",
       "      <td>98836</td>\n",
       "      <td>0.250162</td>\n",
       "      <td>50625</td>\n",
       "      <td>0.228516</td>\n",
       "      <td>0.138365</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          user         sex    age    provider       level    verified  \\\n",
       "0  Train_41088  category 0  24853  category 0  category 2  category 0   \n",
       "1  Train_31781  category 1  24859  category 0  category 1  category 0   \n",
       "2  Train_31874  category 0  24895  category 0  category 2  category 0   \n",
       "3  Train_17154  category 0  24846  category 0  category 2  category 0   \n",
       "4  Train_40531  category 1  24908  category 0  category 2  category 0   \n",
       "\n",
       "   using_time regist_type  card_a_cnt  card_b_cnt  card_c_cnt  agreement1  \\\n",
       "0       24731  category 7       24712       24712       24706  category 0   \n",
       "1       24717  category 1       24731       24712       24725  category 0   \n",
       "2       24707  category 1       24712       24712       24706  category 0   \n",
       "3       24732  category 7       24719       24719       24706  category 0   \n",
       "4       24728  category 1       24712       24712       24706  category 0   \n",
       "\n",
       "   op1_cnt  op2_cnt  card_d_cnt  agreement_total  service1_cnt  service1_amt  \\\n",
       "0    24712    24712       24706            24737         24706         24706   \n",
       "1    24731    24712       24706            24749         24706         24706   \n",
       "2    24719    24712       24706            24737         24706         24706   \n",
       "3    24737    24712       24706            24749         24706         24706   \n",
       "4    24725    24712       24706            24761         24706         24706   \n",
       "\n",
       "   service2_cnt  agreement2  agreement3  agreement4  acc_count  \\\n",
       "0         24706  category 1  category 0  category 0      24737   \n",
       "1         24706  category 0  category 0  category 0      24743   \n",
       "2         24706  category 1  category 1  category 0      24725   \n",
       "3         24706  category 0  category 1  category 0      24712   \n",
       "4         24706  category 0  category 1  category 1      24725   \n",
       "\n",
       "   login_cnt_period1  login_cnt_period2  ip_cnt  login_cnt_avg  \\\n",
       "0              25394              25023   24791          24725   \n",
       "1              29840              26251   25011          24737   \n",
       "2              25163              25163   24804          24725   \n",
       "3              25680              25005   24834          24719   \n",
       "4              25577              25048   24761          24743   \n",
       "\n",
       "   login_days_cnt          province              city  balance  balance_avg  \\\n",
       "0           24791  c3e48f852a0da7b6  1b25064aa7fe4945       11          8.0   \n",
       "1           24974  6dd7071b6edc22d2  998bea7dacc1ac2e        1          1.0   \n",
       "2           24829  71c3649e6dfc18fe  a18aa4ca362cb2c9        6          8.0   \n",
       "3           24822  648024953e363510  e174abcd1cd8c033        7          7.0   \n",
       "4           24761  21c43413032d5522  76cc98309376a6d2        2          2.0   \n",
       "\n",
       "   balance1  balance1_avg  balance2  balance2_avg    service3 service3_level  \\\n",
       "0         4           4.0        10             4  category 0            NaN   \n",
       "1         1           1.0         1             1  category 0            NaN   \n",
       "2        11          12.0         6             5  category 0            NaN   \n",
       "3         1           1.0         7             5  category 0            NaN   \n",
       "4         3           3.0         1             1  category 0            NaN   \n",
       "\n",
       "   product1_amount  product2_amount  product3_amount  product4_amount  \\\n",
       "0                2                9                1                0   \n",
       "1                1                1                1                0   \n",
       "2                1                1                1                0   \n",
       "3                2                6                1                0   \n",
       "4                2                2                1                0   \n",
       "\n",
       "   product5_amount  product6_amount  product7_cnt  product7_fail_cnt  label  \\\n",
       "0                0                5         24712              24706    1.0   \n",
       "1                0                1         24706              24706    1.0   \n",
       "2                0                1         24712              24706    0.0   \n",
       "3                0                1         24712              24706    0.0   \n",
       "4                0                1         24712              24706    1.0   \n",
       "\n",
       "     amount_mean     amount_std  amount_sum  amount_max  amount_min  \\\n",
       "0   44079.000000    6399.927734    132237.0     47774.0     36689.0   \n",
       "1   97000.000000   85970.142515    291000.0    195918.0     40310.0   \n",
       "2   28347.708333    4623.104598    680345.0     42553.0     24767.0   \n",
       "3   54089.933333   36418.750442    811349.0    156321.0     36689.0   \n",
       "4  404972.666667  419810.655236   1214918.0    858932.0     30746.0   \n",
       "\n",
       "   product7_fail_ratio  card_cnt  acc_card_ratio  login_cnt  \\\n",
       "0             0.999757     98836        0.250283      50417   \n",
       "1             1.000000     98874        0.250248      56091   \n",
       "2             0.999757     98836        0.250162      50326   \n",
       "3             0.999757     98850        0.249995      50685   \n",
       "4             0.999757     98836        0.250162      50625   \n",
       "\n",
       "   province_label_mean  city_label_mean  \n",
       "0             0.360114         0.445455  \n",
       "1             0.270062         0.263736  \n",
       "2             0.313309         0.295139  \n",
       "3             0.215081         0.236908  \n",
       "4             0.228516         0.138365  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_feature.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:26.381882Z",
     "start_time": "2020-07-24T02:17:26.004297Z"
    }
   },
   "outputs": [],
   "source": [
    "for f in df_feature.select_dtypes('object').columns:\n",
    "    if f not in ['user']:\n",
    "        lbl = LabelEncoder()\n",
    "        df_feature[f] = lbl.fit_transform(df_feature[f].astype(str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:26.508246Z",
     "start_time": "2020-07-24T02:17:26.383961Z"
    }
   },
   "outputs": [],
   "source": [
    "df_feature.to_pickle('data/feature.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:17:26.559771Z",
     "start_time": "2020-07-24T02:17:26.510971Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((47782, 58), (24315, 58))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = df_feature[df_feature.label.notna()].copy()\n",
    "df_test = df_feature[df_feature.label.isna()].copy()\n",
    "\n",
    "df_train.shape, df_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:18:13.633671Z",
     "start_time": "2020-07-24T02:17:26.561721Z"
    },
    "code_folding": [
     25
    ],
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Fold_1 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[100]\ttrain's auc: 0.719676\tvalid's auc: 0.698623\n",
      "[200]\ttrain's auc: 0.731708\tvalid's auc: 0.704727\n",
      "[300]\ttrain's auc: 0.740915\tvalid's auc: 0.708396\n",
      "[400]\ttrain's auc: 0.748864\tvalid's auc: 0.711081\n",
      "[500]\ttrain's auc: 0.756819\tvalid's auc: 0.71332\n",
      "[600]\ttrain's auc: 0.763761\tvalid's auc: 0.714557\n",
      "[700]\ttrain's auc: 0.76987\tvalid's auc: 0.71521\n",
      "[800]\ttrain's auc: 0.775886\tvalid's auc: 0.715716\n",
      "[900]\ttrain's auc: 0.781564\tvalid's auc: 0.716062\n",
      "[1000]\ttrain's auc: 0.786915\tvalid's auc: 0.716472\n",
      "[1100]\ttrain's auc: 0.792077\tvalid's auc: 0.716754\n",
      "[1200]\ttrain's auc: 0.796878\tvalid's auc: 0.717306\n",
      "[1300]\ttrain's auc: 0.801879\tvalid's auc: 0.71751\n",
      "[1400]\ttrain's auc: 0.80624\tvalid's auc: 0.717794\n",
      "[1500]\ttrain's auc: 0.810552\tvalid's auc: 0.717991\n",
      "[1600]\ttrain's auc: 0.814734\tvalid's auc: 0.718238\n",
      "[1700]\ttrain's auc: 0.818738\tvalid's auc: 0.718484\n",
      "[1800]\ttrain's auc: 0.822545\tvalid's auc: 0.718589\n",
      "[1900]\ttrain's auc: 0.826348\tvalid's auc: 0.718748\n",
      "[2000]\ttrain's auc: 0.830026\tvalid's auc: 0.719033\n",
      "[2100]\ttrain's auc: 0.833728\tvalid's auc: 0.719034\n",
      "Early stopping, best iteration is:\n",
      "[2074]\ttrain's auc: 0.832758\tvalid's auc: 0.719091\n",
      "\n",
      "Fold_2 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[100]\ttrain's auc: 0.722257\tvalid's auc: 0.690314\n",
      "[200]\ttrain's auc: 0.734264\tvalid's auc: 0.69538\n",
      "[300]\ttrain's auc: 0.743576\tvalid's auc: 0.698887\n",
      "[400]\ttrain's auc: 0.751837\tvalid's auc: 0.700082\n",
      "[500]\ttrain's auc: 0.759741\tvalid's auc: 0.701756\n",
      "[600]\ttrain's auc: 0.766771\tvalid's auc: 0.702471\n",
      "[700]\ttrain's auc: 0.773164\tvalid's auc: 0.703082\n",
      "[800]\ttrain's auc: 0.778918\tvalid's auc: 0.70363\n",
      "[900]\ttrain's auc: 0.784517\tvalid's auc: 0.704298\n",
      "[1000]\ttrain's auc: 0.789973\tvalid's auc: 0.704743\n",
      "[1100]\ttrain's auc: 0.794977\tvalid's auc: 0.704949\n",
      "[1200]\ttrain's auc: 0.799815\tvalid's auc: 0.70517\n",
      "[1300]\ttrain's auc: 0.804485\tvalid's auc: 0.705459\n",
      "[1400]\ttrain's auc: 0.808957\tvalid's auc: 0.705617\n",
      "[1500]\ttrain's auc: 0.813566\tvalid's auc: 0.705776\n",
      "Early stopping, best iteration is:\n",
      "[1469]\ttrain's auc: 0.812071\tvalid's auc: 0.705844\n",
      "\n",
      "Fold_3 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[100]\ttrain's auc: 0.717437\tvalid's auc: 0.70139\n",
      "[200]\ttrain's auc: 0.730198\tvalid's auc: 0.707454\n",
      "[300]\ttrain's auc: 0.740402\tvalid's auc: 0.711534\n",
      "[400]\ttrain's auc: 0.748557\tvalid's auc: 0.71342\n",
      "[500]\ttrain's auc: 0.756268\tvalid's auc: 0.715214\n",
      "[600]\ttrain's auc: 0.76298\tvalid's auc: 0.716492\n",
      "[700]\ttrain's auc: 0.769242\tvalid's auc: 0.717659\n",
      "[800]\ttrain's auc: 0.775061\tvalid's auc: 0.718618\n",
      "[900]\ttrain's auc: 0.780928\tvalid's auc: 0.719505\n",
      "[1000]\ttrain's auc: 0.786656\tvalid's auc: 0.720205\n",
      "[1100]\ttrain's auc: 0.792296\tvalid's auc: 0.720691\n",
      "[1200]\ttrain's auc: 0.797583\tvalid's auc: 0.721018\n",
      "[1300]\ttrain's auc: 0.802405\tvalid's auc: 0.721196\n",
      "[1400]\ttrain's auc: 0.806818\tvalid's auc: 0.721383\n",
      "[1500]\ttrain's auc: 0.81112\tvalid's auc: 0.721583\n",
      "Early stopping, best iteration is:\n",
      "[1505]\ttrain's auc: 0.811323\tvalid's auc: 0.721613\n",
      "\n",
      "Fold_4 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[100]\ttrain's auc: 0.718224\tvalid's auc: 0.699113\n",
      "[200]\ttrain's auc: 0.729852\tvalid's auc: 0.70509\n",
      "[300]\ttrain's auc: 0.739292\tvalid's auc: 0.709639\n",
      "[400]\ttrain's auc: 0.748309\tvalid's auc: 0.712992\n",
      "[500]\ttrain's auc: 0.756347\tvalid's auc: 0.715266\n",
      "[600]\ttrain's auc: 0.763052\tvalid's auc: 0.716667\n",
      "[700]\ttrain's auc: 0.769114\tvalid's auc: 0.717383\n",
      "[800]\ttrain's auc: 0.774498\tvalid's auc: 0.717815\n",
      "[900]\ttrain's auc: 0.779667\tvalid's auc: 0.718297\n",
      "[1000]\ttrain's auc: 0.784717\tvalid's auc: 0.71851\n",
      "[1100]\ttrain's auc: 0.789656\tvalid's auc: 0.718776\n",
      "[1200]\ttrain's auc: 0.794468\tvalid's auc: 0.71895\n",
      "[1300]\ttrain's auc: 0.799379\tvalid's auc: 0.718917\n",
      "Early stopping, best iteration is:\n",
      "[1268]\ttrain's auc: 0.797941\tvalid's auc: 0.719025\n",
      "\n",
      "Fold_5 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "[100]\ttrain's auc: 0.719078\tvalid's auc: 0.691699\n",
      "[200]\ttrain's auc: 0.731315\tvalid's auc: 0.699463\n",
      "[300]\ttrain's auc: 0.741124\tvalid's auc: 0.703639\n",
      "[400]\ttrain's auc: 0.749626\tvalid's auc: 0.70636\n",
      "[500]\ttrain's auc: 0.757368\tvalid's auc: 0.708068\n",
      "[600]\ttrain's auc: 0.764539\tvalid's auc: 0.709332\n",
      "[700]\ttrain's auc: 0.770966\tvalid's auc: 0.710214\n",
      "[800]\ttrain's auc: 0.776975\tvalid's auc: 0.711057\n",
      "[900]\ttrain's auc: 0.782506\tvalid's auc: 0.711559\n",
      "[1000]\ttrain's auc: 0.788083\tvalid's auc: 0.711759\n",
      "Early stopping, best iteration is:\n",
      "[1038]\ttrain's auc: 0.790064\tvalid's auc: 0.711953\n"
     ]
    }
   ],
   "source": [
    "ycol = 'label'\n",
    "feature_names = list(\n",
    "    filter(lambda x: x not in [ycol, 'user'], df_train.columns))\n",
    "\n",
    "model = lgb.LGBMClassifier(objective='binary',\n",
    "                           boosting_type='gbdt',\n",
    "                           num_leaves=32,\n",
    "                           max_depth=6,\n",
    "                           learning_rate=0.01,\n",
    "                           n_estimators=10000,\n",
    "                           subsample=0.8,\n",
    "                           feature_fraction=0.6,\n",
    "                           reg_alpha=10,\n",
    "                           reg_lambda=12,\n",
    "                           random_state=seed,\n",
    "                           is_unbalance=True,\n",
    "                           metric='auc')\n",
    "\n",
    "df_oof = df_train[['user', ycol]].copy()\n",
    "df_oof['prob'] = 0\n",
    "prediction = df_test[['user']]\n",
    "prediction['prob'] = 0\n",
    "df_importance_list = []\n",
    "\n",
    "kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)\n",
    "for fold_id, (trn_idx, val_idx) in enumerate(\n",
    "        kfold.split(df_train[feature_names], df_train[ycol])):\n",
    "    X_train = df_train.iloc[trn_idx][feature_names]\n",
    "    Y_train = df_train.iloc[trn_idx][ycol]\n",
    "\n",
    "    X_val = df_train.iloc[val_idx][feature_names]\n",
    "    Y_val = df_train.iloc[val_idx][ycol]\n",
    "\n",
    "    print('\\nFold_{} Training ================================\\n'.format(\n",
    "        fold_id + 1))\n",
    "\n",
    "    lgb_model = model.fit(X_train,\n",
    "                          Y_train,\n",
    "                          eval_names=['train', 'valid'],\n",
    "                          eval_set=[(X_train, Y_train), (X_val, Y_val)],\n",
    "                          verbose=100,\n",
    "                          early_stopping_rounds=50)\n",
    "\n",
    "    pred_val = lgb_model.predict_proba(\n",
    "        X_val, num_iteration=lgb_model.best_iteration_)[:, 1]\n",
    "    df_oof.loc[val_idx, 'prob'] = pred_val\n",
    "\n",
    "    pred_test = lgb_model.predict_proba(\n",
    "        df_test[feature_names], num_iteration=lgb_model.best_iteration_)[:, 1]\n",
    "    prediction['prob'] += pred_test / kfold.n_splits\n",
    "\n",
    "    df_importance = pd.DataFrame({\n",
    "        'column': feature_names,\n",
    "        'importance': lgb_model.feature_importances_,\n",
    "    })\n",
    "    df_importance_list.append(df_importance)\n",
    "\n",
    "    del lgb_model, pred_val, pred_test, X_train, Y_train, X_val, Y_val\n",
    "    gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:18:13.658476Z",
     "start_time": "2020-07-24T02:18:13.635468Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column</th>\n",
       "      <th>importance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>amount_mean</td>\n",
       "      <td>2697.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>amount_min</td>\n",
       "      <td>2442.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>amount_sum</td>\n",
       "      <td>2433.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>city_label_mean</td>\n",
       "      <td>2322.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>amount_std</td>\n",
       "      <td>2041.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>amount_max</td>\n",
       "      <td>1955.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>city</td>\n",
       "      <td>1953.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>age</td>\n",
       "      <td>1867.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>province_label_mean</td>\n",
       "      <td>1867.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>login_cnt_period1</td>\n",
       "      <td>1713.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>using_time</td>\n",
       "      <td>1707.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>login_cnt_period2</td>\n",
       "      <td>1501.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>acc_card_ratio</td>\n",
       "      <td>1295.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>login_cnt</td>\n",
       "      <td>1284.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>province</td>\n",
       "      <td>1258.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>balance_avg</td>\n",
       "      <td>1186.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>ip_cnt</td>\n",
       "      <td>1131.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>login_days_cnt</td>\n",
       "      <td>1100.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>login_cnt_avg</td>\n",
       "      <td>1078.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>agreement_total</td>\n",
       "      <td>809.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>balance1_avg</td>\n",
       "      <td>773.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>product7_fail_cnt</td>\n",
       "      <td>750.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>product7_fail_ratio</td>\n",
       "      <td>701.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>balance2</td>\n",
       "      <td>693.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>product2_amount</td>\n",
       "      <td>669.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>product6_amount</td>\n",
       "      <td>666.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>op1_cnt</td>\n",
       "      <td>646.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>op2_cnt</td>\n",
       "      <td>620.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>balance</td>\n",
       "      <td>613.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>product7_cnt</td>\n",
       "      <td>565.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>acc_count</td>\n",
       "      <td>529.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>card_cnt</td>\n",
       "      <td>518.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>balance1</td>\n",
       "      <td>391.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>level</td>\n",
       "      <td>315.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>card_c_cnt</td>\n",
       "      <td>313.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>regist_type</td>\n",
       "      <td>301.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>sex</td>\n",
       "      <td>301.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>balance2_avg</td>\n",
       "      <td>277.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>agreement3</td>\n",
       "      <td>271.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>agreement2</td>\n",
       "      <td>264.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>provider</td>\n",
       "      <td>236.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>card_a_cnt</td>\n",
       "      <td>220.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>card_d_cnt</td>\n",
       "      <td>206.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>card_b_cnt</td>\n",
       "      <td>176.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>agreement4</td>\n",
       "      <td>132.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>service3_level</td>\n",
       "      <td>131.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>service3</td>\n",
       "      <td>111.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>product3_amount</td>\n",
       "      <td>93.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>verified</td>\n",
       "      <td>88.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>product1_amount</td>\n",
       "      <td>64.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>service1_amt</td>\n",
       "      <td>13.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>service2_cnt</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>product5_amount</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>service1_cnt</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>agreement1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>product4_amount</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 column  importance\n",
       "0           amount_mean      2697.8\n",
       "1            amount_min      2442.8\n",
       "2            amount_sum      2433.2\n",
       "3       city_label_mean      2322.4\n",
       "4            amount_std      2041.6\n",
       "5            amount_max      1955.0\n",
       "6                  city      1953.0\n",
       "7                   age      1867.8\n",
       "8   province_label_mean      1867.8\n",
       "9     login_cnt_period1      1713.2\n",
       "10           using_time      1707.4\n",
       "11    login_cnt_period2      1501.0\n",
       "12       acc_card_ratio      1295.6\n",
       "13            login_cnt      1284.8\n",
       "14             province      1258.8\n",
       "15          balance_avg      1186.0\n",
       "16               ip_cnt      1131.2\n",
       "17       login_days_cnt      1100.2\n",
       "18        login_cnt_avg      1078.8\n",
       "19      agreement_total       809.2\n",
       "20         balance1_avg       773.6\n",
       "21    product7_fail_cnt       750.0\n",
       "22  product7_fail_ratio       701.8\n",
       "23             balance2       693.2\n",
       "24      product2_amount       669.0\n",
       "25      product6_amount       666.8\n",
       "26              op1_cnt       646.2\n",
       "27              op2_cnt       620.8\n",
       "28              balance       613.6\n",
       "29         product7_cnt       565.8\n",
       "30            acc_count       529.6\n",
       "31             card_cnt       518.8\n",
       "32             balance1       391.8\n",
       "33                level       315.0\n",
       "34           card_c_cnt       313.4\n",
       "35          regist_type       301.6\n",
       "36                  sex       301.4\n",
       "37         balance2_avg       277.8\n",
       "38           agreement3       271.4\n",
       "39           agreement2       264.6\n",
       "40             provider       236.6\n",
       "41           card_a_cnt       220.6\n",
       "42           card_d_cnt       206.0\n",
       "43           card_b_cnt       176.0\n",
       "44           agreement4       132.4\n",
       "45       service3_level       131.6\n",
       "46             service3       111.4\n",
       "47      product3_amount        93.4\n",
       "48             verified        88.4\n",
       "49      product1_amount        64.2\n",
       "50         service1_amt        13.2\n",
       "51         service2_cnt         0.8\n",
       "52      product5_amount         0.2\n",
       "53         service1_cnt         0.0\n",
       "54           agreement1         0.0\n",
       "55      product4_amount         0.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_importance = pd.concat(df_importance_list)\n",
    "df_importance = df_importance.groupby([\n",
    "    'column'\n",
    "])['importance'].agg('mean').sort_values(ascending=False).reset_index()\n",
    "df_importance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:18:13.681761Z",
     "start_time": "2020-07-24T02:18:13.660038Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auc: 0.7154059918761352\n"
     ]
    }
   ],
   "source": [
    "auc = roc_auc_score(df_oof[ycol], df_oof['prob'])\n",
    "print('auc:', auc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-24T02:18:13.763418Z",
     "start_time": "2020-07-24T02:18:13.683347Z"
    }
   },
   "outputs": [],
   "source": [
    "os.makedirs('sub', exist_ok=True)\n",
    "prediction.to_csv('sub/yizhifu_{}.csv'.format(auc), index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:ml]",
   "language": "python",
   "name": "conda-env-ml-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
